package concurrent.lock;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @program JavaBase
 * @description: 简单的线程安全list
 * @author: zhanglu
 * @create: 2021-05-22 19:07:00
 */
public class LuReentrantReadWriteLock<T> {

    //线程不安全的list
    private List<T> array = new ArrayList<>();
    //独占锁
    private volatile ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
    ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();

    public void add(T e){
        writeLock.lock();
        try {
            array.add(e);
        } finally {
            writeLock.unlock();
        }
    }

    public void remove(T e){
        writeLock.lock();
        try {
            array.remove(e);
        } finally {
            writeLock.unlock();
        }
    }

    public T get(int index){
        readLock.lock();
        try {
            return array.get(index);
        } finally {
            readLock.unlock();
        }
    }

    public static void main(String[] args) {
        int EXCLUSIVE_MASK = (1 << 16) - 1;
        System.out.println(EXCLUSIVE_MASK);
        System.out.println(65536 & (EXCLUSIVE_MASK));
        System.out.println(65536 >>> 16);
    }

}
